package com.starbaba.module.view;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

import javax.annotation.Resource;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.google.gson.JsonObject;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.common.ResultJson;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.module.model.ActivityModel;
import com.starbaba.module.model.LoanContentItemWithVal;
import com.starbaba.module.service.ActivityVarsService;
import com.starbaba.redis.JedisUtils;

@SuppressWarnings("rawtypes")
public class ActivityAction extends BaseAction implements ModelDriven {
	
	private static final long serialVersionUID = 1L;

	private ActivityModel activityModel;
	
	@Resource(name="activityVarsServiceImpl")
	private ActivityVarsService activityVarsService;
	
	//查询活动字段的方法--先查字段key,再查字段值
	public void activityVarsCheck() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		//获取活动/模板id--aid/mid
		String mid = (String) paramsMap.get("mid");
		String aid = (String) paramsMap.get("aid");
		if(mid == null){
			sendErrMsg("模板id为空");
			return;
		}
		if(aid == null){
			sendErrMsg("活动id为空");
			return;
		}
		try {
			//获取key
			List<LoanContentItemWithVal> activityVarsKeyList = activityVarsService.getKeyList(Long.valueOf(mid));
			
			if(activityVarsKeyList.size() == 0){
				sendJsonMessage("null");
				return;
			}
			
			//以key获取值
			List<LoanContentItemWithVal> activityVarsValList = activityVarsService.getValList(activityVarsKeyList, Integer.valueOf(aid));
			
			String jsonStr = JsonUtils.toJson(activityVarsValList, null, true, null, false);
			sendJsonMessage(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//活动字段值修改的方法--无论是修改还是添加流程都是先删除后添加
	@SuppressWarnings("deprecation")
    public void activityVarsUpdate() throws AppException, IOException{
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);

		//获取活动id--aid
		String aid = (String) paramsMap.get("aid");
		if(aid == null){
			sendErrMsg("活动id为空");
			return;
		}
		
		BaseDao dao = new BaseDao(Constant.POOL_LIFE);
		
		Boolean hasData = false;
		
		String addSql = "INSERT INTO loan_content_activity_vars(aid, content, val, ctime) VALUES ";
		
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String currentTime = dateformat.format(new Date());
		LogService.info("paramsMap======" + paramsMap);
		java.util.Set<Entry<String, Object>> set = paramsMap.entrySet();
		for (Entry<String, Object> entry : set) {
			String key = entry.getKey();
			Object value = entry.getValue();
			if(!key.equalsIgnoreCase("aid")){
				addSql += "(" + aid + ", '" + key + "', '" + value + "', '" + currentTime + "'),";
				if(!hasData){
					hasData = true;
				}
			}
		}
		
		if(!hasData){
			sendErrMsg("请填写信息");
			return;
		}
		
		//删除最后一个,
		addSql = addSql.substring(0, addSql.length() - 1);
		
		//删除这个aid下的全部数据
		String delSql = "DELETE FROM loan_content_activity_vars WHERE aid = ?";
		List<Object> delList = new ArrayList<Object>();
		delList.add(aid);
		dao.delete(delSql, delList);
		
		List<Object> addList = new ArrayList<Object>();
		dao.add(addSql, addList);
		JedisUtils.del("loan_new_model_" + aid);
		sendErrMsg("保存成功");
	}
	
	@Override
	public ActivityModel getModel() {
		// TODO Auto-generated method stub
		if (activityModel == null) {
			activityModel = new ActivityModel();
        }
        return activityModel;
	}
	
	private void sendErrMsg(String msg) {
		JsonObject result = new JsonObject();
		ResultJson resultJs = new ResultJson();
		resultJs.msg = msg;
		result.add("result", resultJs.toJSON());
		try {
			sendJsonMessage(result.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
